package basis;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Test;

/**
 * 统计字符出现次数,只出现一次的字符,取出字符串中的数字与日期
 * 
 * @author 71936_000
 * 
 */
public class Demo7_CharacterString {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入字符串");
		String str = sc.nextLine();
		System.out.println("要知道哪个字符出现的次数");
		String s = sc.nextLine();
		char c = s.charAt(0);
		Demo7_CharacterString dc = new Demo7_CharacterString();
		System.out.println(c + "在字符串中出现了" + dc.time(str, c) + "次");
		dc.once(str);
		dc.number(str);
		dc.test(str);
		dc.time1(str);
	}

	// 字符出现的次数
	public int time(String str, char c) {
		int num = 0;
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == c) {
				num++;
			}
		}
		return num;
	}

	// 出现一次的字符
	public void once(String str) {
		String str1 = str;
		System.out.print("出现了一次的字符有：");
		for (int i = 0; i < str.length(); i++) {
			int num = 0;
			for (int j = 0; j < str1.length(); j++) {
				if (str.charAt(i) == str1.charAt(j)) {
					num++;
				}
			}
			if (num == 1) {
				System.out.print(str.charAt(i) + "\t");
			}
		}
		System.out.println();

	}

	//每个 字符出现的次数
	public void time1(String str){
		String str1 = str;
		for (int i = 0; i < str.length(); i++) {
			int num = 0;
			for (int j = 0; j < str1.length(); j++) {
				if (str.charAt(i) == str1.charAt(j)) {
					num++;
				}
			}
			System.out.println(str.charAt(i) +"出现的次数为"+num+ "\t");
		}
		System.out.println();
	}
	
	
	
	
	private void test(String str){
		if(str!=null){
			String[] strs = str.split(",");
			Map<String,Integer> map = new HashMap<String,Integer>();
			for(String a:strs){
				Integer times = map.get(a)==null?0:map.get(a);
				times++;
				map.put(a.intern(), times);
			}
			Iterator<String> it = map.keySet().iterator();
			while(it.hasNext()){
				String a = it.next();
				System.out.println(a+"出现了"+map.get(a)+"次！");
			}
		}
	}
	
	
	// 字符串中出现的数字
	public void number(String str) {
		int h = 0;
		System.out.print("字符串中的数字有：");
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) > 47 && str.charAt(i) < 58) {
				System.out.print(str.charAt(i) + "  ");
				h++;
			}
		}
		if (h == 0) {
			System.out.println("字符串中没有数字出现");
		}
	}
	
	
	
	//取出字符串中的日期
	@Test
	public void date(){
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入字符串");
		String str = sc.nextLine();
		Pattern p=Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
		Matcher m=p.matcher(str);
		if(m.find()){
			System.out.println(m.group());
		}else{
			System.out.println("ddd");
		}
	}
	
	
}
